package wiki.generic.iotests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import wiki.generic.contents.Content;
import wiki.generic.io.ContentsReader;
import wiki.generic.io.Page;
import wiki.generic.med.PagesService;
import wiki.generic.utilities.Substring;

/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 *
 */

public class ConcreteContentsReader implements ContentsReader {
	

	private PagesService ps;

	public ConcreteContentsReader(PagesService ps) {
		super();
		this.ps=ps;
	}

	@Override
	public Page getPage(String pagePath) {
		List<Content> contentList=new ArrayList<Content>();
		File file=new File(pagePath);
		
		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line = reader.readLine();
			while (line != null) {
				createList(line, contentList);
				line = reader.readLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return new Page(contentList);
	}

	
	private void createList(String line, List<Content> list){
		
		Substring sub=new Substring(line, "$$||$$");
		String nome=sub.nextSubString();
		String type = sub.nextSubString();
		int ID= Integer.parseInt(sub.nextSubString());
		List<String> attributes = new ArrayList<String>();
		while (sub.hasMore()) {
			attributes.add(sub.nextSubString());
		}
		Content c=ps.generateContent(nome,type,ID, attributes);
		list.add(c);
	}

}
